package org.snackframework.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ThroughUrlCacheElement extends Cache
{
	private static Log log = LogFactory.getLog(ThroughUrlCacheElement.class);
	
	protected static Object lock=new Object();
	
	private ViewMetaData viewMetaData;
	
	private MethodMeta methodMeta;
	
	private boolean singleton=true;

	public ThroughUrlCacheElement(){}
	
	public ThroughUrlCacheElement(ViewMetaData viewMetaData,MethodMeta methodMeta)
	{
		this.viewMetaData=viewMetaData;
		this.methodMeta=methodMeta;
	}
	
	public ThroughUrlCacheElement(ViewMetaData viewMetaData,MethodMeta methodMeta,boolean singleton)
	{
		this.viewMetaData=viewMetaData;
		this.methodMeta=methodMeta;
		this.singleton=singleton;
	}
	
	public ViewMetaData getViewMetaData()
	{
		return viewMetaData;
	}

	public void setViewMetaData(ViewMetaData viewMetaData)
	{
		this.viewMetaData = viewMetaData;
	}

	public MethodMeta getMethodMeta()
	{
		return methodMeta;
	}

	public void setMethodMeta(MethodMeta methodMeta)
	{
		this.methodMeta = methodMeta;
	}
	
	public void setViewObject()
	{
		synchronized(ThroughUrlCacheElement.lock)
		{
			if(null==viewMetaData.getViewObject())
			{
				try
				{
//					viewMetaData.setViewObject(viewMetaData.getViewClass().newInstance());
					viewMetaData.initializeViewObject();
				}
				catch(IllegalAccessException e)
				{
					if(log.isDebugEnabled())
						e.printStackTrace();
					log.error("反射实例化非法访问",e);
				}
				catch(InstantiationException e)
				{
					if(log.isDebugEnabled())
						e.printStackTrace();
					log.error("反射实例化失败",e);
				}
			}
		}
	}
	
	public boolean isInstantiated()
	{
		boolean isExsit=false;
		if(this.viewMetaData.getViewObject()!=null)
		{
			isExsit=true;
		}
		return isExsit;
	}

	public boolean isSingleton()
	{
		return this.singleton;
	}
}
